package org.catdroid.bcnappsjam.barcelonaparticipa;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.util.Log;

public class NfcActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
		Ndef ndefTag = Ndef.get(tag);

		try {
			ndefTag.connect();
			NdefRecord ndefRecord = ndefTag.getNdefMessage().getRecords()[0];
			String s = new String(ndefRecord.getPayload());
			Log.i("NFC", "llegit: " + s);
			Uri u = Uri.parse(s);
			String resposta = u.getPath().replaceAll("/", "");

			Intent i = new Intent(this, SendingActivity.class);
			i.putExtra(SendingActivity.EXTRA_OPINIO_ID, resposta);
			startActivity(i);
		} catch (Exception e) {
			Log.e("NFC", "error", e);
		}
	}
}
